package com.itheima.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet("/aServlet")
public class AServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("AServlet doGet....");
        String name = "张三";
        // URL编码
        String utfName = URLEncoder.encode(name,"utf-8");
        System.out.println("utfName:"+utfName);
        // 构建一个Cookie,千万不要写中文
        Cookie cookie1 = new Cookie("addr", utfName);
        // 单位秒
        cookie1.setMaxAge(60*60);
        Cookie cookie2 = new Cookie("email", "zs@163.com");
        cookie2.setMaxAge(60*60*24*365);
        // 响应客户端
        resp.addCookie(cookie1);
        //resp.setHeader("Set-Cookie","email=\"zs@163.com\"; Version=1; Max-Age=31536000; Expires=Wed, 22-Nov-2023 03:39:28 GMT");
        resp.addCookie(cookie2);
    }
}
